<?php
namespace Home\Controller;		//这个目录下的文件
use Think\Controller;			//运用这个地方的基类
class UserController extends Controller {
	

    public function index(){
        $sql="select title,create_time,answer_num,view_num from question where uid='{$_SESSION['uid']}'";
        $res=M()->query($sql);
        $this->assign('res',$res);
        var_dump($res);



        $this->display();		//引入登录html的函数

    }
    public function set(){      //设置的我的资料的页面
    	$sql="select * from user where id='{$_SESSION['uid']}'";
    	$res=M()->query($sql);
    	$res1=$res[0];
    	$this->assign('res',$res1);	//将局部变量分配到html
        $this->display();


    }
    public function loginout()
    {
    	session_unset();		//释放所有session变量
    	session_destroy();		//删除当前用户服务器的session文件
    	$this->success('退出成功',U('home/index/index'));
    }
    public function face(){             //头像页面
        $this->display();		
    }
    public function password(){         //密码页面
        $this->display();		
    }
    public function number(){           //账号绑定
        $this->display();		
    }
    public function change_user(){
    	$change_user=I('post.');
    	$change_user['nikename']=$change_user['username'];
    	$change_user['signname']=$change_user['sign'];

    	$sql_email="select * from user where email= '{$change_user['email']}'";
    	$res_email=M()->query($sql_email);

    	$sql2_email="select * from user where nikename= '{$change_user['nikename']}'";
    	$res2_email=M()->query($sql2_email);

    	if ($res_email and $res_email[0]['id']!=$_SESSION['uid']) {
    		 $arr=array(
        		'error'=>1,
        		'info'=>'邮箱已存在'
        		);
        	echo json_encode($arr);
        	exit;
    	}

    	if ($res2_email and $res2_email[0]['id']!=$_SESSION['uid']) {
    		 $arr=array(
        		'error'=>1,
        		'info'=>'昵称已存在'
        		);
        	echo json_encode($arr);
        	exit;
    	}

    	if ($change_user['sign']) {
    		$_SESSION['signname']=$change_user['sign'];

    	};
 
        $res=M('user')->where("id=".$_SESSION['uid'])->save($change_user);
        if ($res) {
        	$arr=array(
        		'error'=>0,
        		'info'=>'更新成功'
        		);
        	echo json_encode($arr);
        }else{
        	$arr=array(
        		'error'=>1,
        		'info'=>'更新失败'
        		);
        	echo json_encode($arr);
        }
        
	}
	public function change_pass(){
		$get=$_POST;
		$sql="select * from user where id='{$_SESSION['uid']}'";
		$res=M()->query($sql);
		$reg = "/^[a-zA-Z0-9_]+$/";
    	$preg=preg_match($reg, $get['pass']);
    	if (!$preg) {
    		$arr=array(
				'error'=>1,
				'info'=>"新密码格式错误"
				);
			echo json_encode($arr);
    	}else{
    		if ($get['pass'] != $get['repass']) {
			$arr=array(
				'error'=>1,
				'info'=>"'确认密码'错误"
				);
			echo json_encode($arr);
			exit;
		}else{
			$nowpass=md5($get['nowpass']);
			if ($nowpass != $res[0]['password']) {
				$arr=array(
				'error'=>1,
				'info'=>'密码错误'
				);
				echo json_encode($arr);
			}else{
				$newpass=md5($get['repass']);
				$change_sql = "update user set password='{$newpass}' where id='{$_SESSION['uid']}'";
				$res = M()->execute($change_sql);
				$arr=array(
				'error'=>0,
				'info'=>'密码修改成功'
				);
				unset($_SESSION["password"]);
				session_destroy();
				echo json_encode($arr);
			}
		}
    	}





   



	}

    public function update(){
        $new=new \Think\Upload();	//实例化上传类
        $new->rootPath  = './uploads/'; // 设置附件上传根目录
    	$upload->maxSize   =     3145728 ;// 设置附件上传大小
   		$upload->exts      =     array( 'gif', 'png', 'jpeg');

   		$info = $new->upload();	//使用内置上传函数上传文件
    	
        if (!$info) {
        	echo $new->getError();
        }else{
        	// 上传的文件所放的路径
    		$path = "Uploads/".$info['images']['savepath'].$info['images']['savename'];
    		$sql = "update user set face='{$path}' where id='{$_SESSION['uid']}'";
    		$res = M()->execute($sql);	//执行修改mysql行的命令
    		if ($res) {
    			$_SESSION['face'] = $path; 	//更新session里面的图像
    			$arr=array(
    				'error'=>0,
    				'info'=>'更新图像成功',
    				'face'=>$path 		//传输地址过去做同步更新页面图像
    				);
    			echo json_encode($arr);
    		}else{
    			$arr=array(
    				'error'=>1,
    				'info'=>'更新图像失败'
    				);
    			echo json_encode($arr);
    		}
	   	      		
        }
        
    }


    public function myhomepage(){       //我的主页
       $this->display();

    }

    public function mynews(){        //我的消息
        $sql="select t3.nikename,t1.title,t2.create_time,t1.id from question t1 inner join answer t2 on t1.id=t2.qid inner join user t3 on t2.uid=t3.id where t1.uid='{$_SESSION['uid']}'";
        $res=M()->query($sql);
        $this->assign('res',$res);
        var_dump($res);

        $this->display();

    }
    public function userhomepage(){        //用户的主页
        $uid=I('get.uid');
        $sql="select * from user where id='{$uid}'";
        $res1=M()->query($sql);
        $this->assign('res1',$res1);

      
        $question_sql="select t2.title,t2.create_time,t2.answer_num,t2.view_num from user t1 inner join question t2 on t1.id=t2.uid where t1.id='{$uid}'";
        $res2=M()->query($question_sql);
        $this->assign('res2',$res2);

        $answer_sql="select t2.content,t2.create_time,t3.title from user t1 inner join answer t2 on t1.id=t2.uid inner join question t3 on t2.qid=t3.id where t1.id='{$uid}'";
        $res3=M()->query($answer_sql);
        $this->assign('res3',$res3);



        $this->display();

    }







     







}
